﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AiXiu.DAL;
using AiXiu.Common;
using AiXiu.Model;
using AiXiu.IBLL;

namespace AiXiu.BLL
{
    public class LikeBLL : ILikeBLL
    {
        private string videoId;
        public LikeBLL(string videoId) 
        {
            this.videoId = "VideoLike_" + videoId;
        }
        public OperResult<bool> AddOrRemoveLike(int UserId)
        {
            //如果该用户已经点赞过该视频，执行取消点赞操作
            if (IsLike(UserId) == 1)
            {
                if (RedisHelper.SetRemove(this.videoId, UserId))
                {
                    return OperResult<bool>.Succeed(false);
                }
                else
                {
                    return OperResult<bool>.Failed("取消点赞失败");
                }
            }
            else
            {
                if (RedisHelper.SetAdd(this.videoId, UserId))
                {
                    return OperResult<bool>.Succeed(true);
                }
                else
                {
                    return OperResult<bool>.Failed("点赞失败");
                }
            }
        }

        public int IsLike(int UserId)
        {
            return RedisHelper.SetContains(videoId, UserId) ? 1 : 0;
        }

        public long LikeCount()
        {
            return RedisHelper.SetLength(videoId);
        }
    }
}
